# WSL2 + Ubuntu 22.04 安装脚本
# 按照手册要求配置开发环境

Write-Host "=== RISC-V 操作系统开发环境安装脚本 ===" -ForegroundColor Green
Write-Host "按照手册要求：推荐开发平台 Ubuntu 22.04 LTS（或 WSL2）" -ForegroundColor Yellow

Write-Host "`n步骤1: 检查WSL状态" -ForegroundColor Cyan
wsl --status

Write-Host "`n步骤2: 安装WSL2和Ubuntu 22.04" -ForegroundColor Cyan
Write-Host "注意：此步骤需要管理员权限" -ForegroundColor Red
Write-Host "请在管理员PowerShell中运行以下命令：" -ForegroundColor Yellow
Write-Host "wsl --install -d Ubuntu-22.04" -ForegroundColor White

Write-Host "`n步骤3: 安装完成后，进入WSL环境" -ForegroundColor Cyan
Write-Host "wsl -d Ubuntu-22.04" -ForegroundColor White

Write-Host "`n步骤4: 在Ubuntu中安装基础依赖" -ForegroundColor Cyan
Write-Host "sudo apt-get update" -ForegroundColor White
Write-Host "sudo apt-get install -y build-essential git python3 qemu-system-misc expect gdb-multiarch" -ForegroundColor White

Write-Host "`n步骤5: 安装RISC-V工具链" -ForegroundColor Cyan
Write-Host "# 方案A：使用预编译包（推荐）" -ForegroundColor Yellow
Write-Host "wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2023.07.07/riscv64-elf-ubuntu-20.04-gcc-nightly-2023.07.07-nightly.tar.gz" -ForegroundColor White
Write-Host "sudo tar -xzf riscv64-elf-ubuntu-20.04-gcc-nightly-2023.07.07-nightly.tar.gz -C /opt/" -ForegroundColor White
Write-Host "echo 'export PATH=\"/opt/riscv/bin:\$PATH\"' >> ~/.bashrc" -ForegroundColor White
Write-Host "source ~/.bashrc" -ForegroundColor White

Write-Host "`n步骤6: 验证安装" -ForegroundColor Cyan
Write-Host "riscv64-unknown-elf-gcc --version" -ForegroundColor White
Write-Host "qemu-system-riscv64 --version" -ForegroundColor White

Write-Host "`n步骤7: 获取xv6参考源码" -ForegroundColor Cyan
Write-Host "git clone https://github.com/mit-pdos/xv6-riscv.git" -ForegroundColor White
Write-Host "cd xv6-riscv && make qemu  # 验证能否正常运行" -ForegroundColor White

Write-Host "`n=== 安装完成后的验证命令 ===" -ForegroundColor Green
Write-Host "riscv64-unknown-elf-gcc --version" -ForegroundColor White
Write-Host "qemu-system-riscv64 --version" -ForegroundColor White
Write-Host "make --version" -ForegroundColor White
Write-Host "git --version" -ForegroundColor White
Write-Host "python3 --version" -ForegroundColor White


